﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SvD_Templates;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public datosTemplate dt;
        private fnXML xml = new fnXML();

        public void resetTodo()
        {
            titleWordCounter.Text = "Left: 80";
            dt.clear();
            load_dt_to_application(dt);


            //Ticket Provided test
            checkTicket.Checked = true;
            cajaTicket.ResetText();
        }

        #region Function Keys
        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    String _exit_Confirm = MessageBox.Show("Do you want to reset the template?", "Exit Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
                    if (_exit_Confirm == "Yes")
                        resetTodo();
                    break;

                case Keys.F1:
                    crearTemplate();
                    break;

                case Keys.F2:
                    selectMainTemplate sMTemp = new selectMainTemplate();
                    sMTemp._dt = dt;
                    sMTemp.frm1 = this;
                    sMTemp.ShowDialog();
                    break;

                case Keys.F3:
                    recuperarTemplate rTemp = new recuperarTemplate();
                    rTemp.frm1 = this;
                    rTemp.ShowDialog();
                    break;

                case Keys.F5:
                    mainTemplate mTemp = new mainTemplate();
                    mTemp._dt = dt;
                    mTemp.ShowDialog();
                    break;

                case Keys.F6:
                    extraTemplate eTemp = new extraTemplate();
                    eTemp._dt = dt;
                    eTemp.ShowDialog();
                    break;

                case Keys.F7:
                    SeverityQuestions sevQuest = new SeverityQuestions();
                    sevQuest._dt = dt;
                    sevQuest.ShowDialog();
                    break;

                case Keys.F8:
                    EKMSearch ekms = new EKMSearch();
                    ekms.frm1 = this;
                    ekms.ShowDialog();
                    break;

                case Keys.F12:
                    contextMenuStrip1.Show();
                    break;
            }

            return base.ProcessDialogKey(keyData);
        }
        #endregion

        #region Borderless Window
        protected override void WndProc(ref Message m)
        {
            // Let the base class have first crack
            base.WndProc(ref m);
            int WM_NCHITTEST = 0x84; // winuser.h
            // If the user clicked on the client area,
            // ask the OS to treat it as a click on the caption
            if (m.Msg != WM_NCHITTEST) return;
            int HTCLIENT = 1;
            int HTCAPTION = 2;
            if (m.Result.ToInt32() == HTCLIENT)
                m.Result = (IntPtr)HTCAPTION;
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
            dt = new datosTemplate();
            resetTodo();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                foreach (String contacto in xml.getPhonebook())
                    cajaWarmTransfer.AutoCompleteCustomSource.Add(contacto);
                foreach (String inbox in xml.getInboxes())
                    cajaDespacho.AutoCompleteCustomSource.Add(inbox);
                foreach (String site in xml.getSites())
                    cajaTitulo.AutoCompleteCustomSource.Add(site);
                foreach (String company in xml.getCompanies())
                    cajaCompany.AutoCompleteCustomSource.Add(company);
                foreach (String keyword in xml.getKeywords())
                    cajaKeyword.AutoCompleteCustomSource.Add(keyword);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problema al cargar la informacion del servidor");
            }
        }



        private void mBut_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Focus();            
        }

        private void cBut_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void cajaTitulo_KeyDown(object sender, KeyEventArgs e)
        {
            titleWordCounter.Text = "Left: " + (80 - (cajaTitulo.Text.Length)).ToString();
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show(e.Button.ToString());
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            String _exit_Confirm = MessageBox.Show("Do you want to close the application?", "Exit Application", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
            if (_exit_Confirm == "Yes")
                Application.Exit();
        }

        private void crearTemplate()
        {
            Template Template = new Template();

            // Titulo del ticket
            dt.Titulo = cajaTitulo.Text;

            // User Company
            // temp.user_Company = cajaCompania.Text;
            // Cambiarlo a Combo Box - temp.Severidad = comboSeveridad.SelectedItem.ToString();
            dt.user_Company = cajaCompany.Text;


            // Verificar Asset
            if (checkAsset.Checked && rCheckAssetSi.Checked)
                dt.Verificar_Asset = "Yes, Asset Validated on Service Center";
            else if (checkAsset.Checked && rCheckAssetNo.Checked)
                dt.Verificar_Asset = "No, ASSET WAS NOT FOUND IN AC, PLEASE VALIDATE ASSET";
            if (!checkAsset.Checked)
                dt.Verificar_Asset = "No";

            // Verificar SSN o RFC
            dt.Verificar_SSNRFC = (checkSSNRFC.Checked) ? dt.Verificar_SSNRFC = "Yes" : dt.Verificar_SSNRFC = "No";

            // es un IMAC?
            dt.Es_IMAC = (checkIMAC.Checked) ? "Yes" : "No";

            // Idioma
            dt.Idioma = cajaIdioma.Text;


            // KeyWord
            dt.Keyword = (cajaKeyword.Text == "") ? "N/A" : dt.Keyword = cajaKeyword.Text;

            // Detalles del ticket (Issue)
            dt.Detalles_Ticket = cajaDetallesTicket.Text;

            // Acciones Realizadas (TroubleShooting)
            dt.Acciones_Realizadas = cajaAcciones.Text;

            // Caja a Asignar (Dispatching)
            dt.Caja_Asignacion = (cajaDespacho.Text == "") ? "No" : dt.Caja_Asignacion = cajaDespacho.Text;

            // Severidad
            dt.Severidad = cajaSeveridad.Text;

            // Mejor hora para localizar al Usuario
            dt.Mejor_Hora_Localizar = (cajaHoraLocalizar.Text == "") ? "N/A" : dt.Mejor_Hora_Localizar = cajaHoraLocalizar.Text;

            // Contacto Alterno o Telefono Alterno
            dt.Contact_Alterno = (cajaContactoAlterno.Text == "") ? "N/A" : dt.Contact_Alterno = cajaContactoAlterno.Text;

            // Warm Transfer
            dt.Warm_Transfer = (cajaWarmTransfer.Text == "") ? "No" : dt.Warm_Transfer = cajaWarmTransfer.Text;

            // Template adicional ya esta guardado por default.

            // Informacion Extra ya esta guardado por default.

            // URL
            String _no_url = "Yes";
            if (cajaURL.Text == "")
            {
                _no_url = MessageBox.Show("You are creating a template without URL.\nThis could lead to a mishandled case.\n\nDo you wish to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning).ToString();
                dt.url = ""; //"http://ekmswgi.amer.quick2know.eds.com/EKMS/WGI_Delphi_US/";
            }
            else dt.url = cajaURL.Text;

            // Ticket Otorgado
            dt.Ticket_Otorgado = (checkTicket.Checked) ? "Yes" : dt.Ticket_Otorgado = "No, " + cajaTicket.Text; 

            // Severity Questions
            // Shipments
            if (dt.SQ_Shipments_1 == "") dt.SQ_Shipments_1 = "N/A";
            if (dt.SQ_Shipments_2 == "") dt.SQ_Shipments_2 = "N/A";
            if (dt.SQ_Shipments_3 == "") dt.SQ_Shipments_3 = "N/A";

            // Manufacturing
            if (dt.SQ_Manufacturing_1 == "") dt.SQ_Manufacturing_1 = "N/A";
            if (dt.SQ_Manufacturing_2 == "") dt.SQ_Manufacturing_2 = "N/A";
            if (dt.SQ_Manufacturing_3 == "") dt.SQ_Manufacturing_3 = "N/A";

            // Business
            if (dt.SQ_Business_1 == "") dt.SQ_Business_1 = "N/A";
            if (dt.SQ_Business_2 == "") dt.SQ_Business_2 = "N/A";
            if (dt.SQ_Business_3 == "") dt.SQ_Business_3 = "N/A";

            // Outage
            if (dt.SQ_Outage_1 == "") dt.SQ_Outage_1 = "N/A";
            if (dt.SQ_Outage_2 == "") dt.SQ_Outage_2 = "N/A";
            if (dt.SQ_Outage_3 == "") dt.SQ_Outage_3 = "N/A";

            // Other
            if (dt.SQ_Other_1 == "") dt.SQ_Other_1 = "N/A";
            if (dt.SQ_Other_2 == "") dt.SQ_Other_2 = "N/A";
            if (dt.SQ_Other_3 == "") dt.SQ_Other_3 = "N/A";

            if (_no_url == "Yes")
            {
                Template.CrearTemplate(dt);
                this.WindowState = FormWindowState.Minimized;
            }
        }

        #region Cargar Template a Aplicacion

        public void load_dt_to_application(datosTemplate dt_)
        {
            this.dt = dt_;

            cajaTitulo.Text = dt.Titulo;
            cajaDetallesTicket.Text = dt.Detalles_Ticket;
            cajaAcciones.Text = dt.Acciones_Realizadas;
            cajaCompany.Text = dt.user_Company;
            cajaKeyword.Text = dt.Keyword;
            cajaSeveridad.Text = dt.Severidad;
            cajaIdioma.Text = dt.Idioma;
            cajaDespacho.Text = dt.Caja_Asignacion;
            cajaHoraLocalizar.Text = dt.Mejor_Hora_Localizar;
            cajaContactoAlterno.Text = dt.Contact_Alterno;
            cajaWarmTransfer.Text = dt.Warm_Transfer;
            cajaTicket.Text = dt.Ticket_Otorgado;
            cajaURL.Text = dt.url;

            checkAsset.Checked = (dt.Verificar_Asset == "yes") ? true : false;
            checkSSNRFC.Checked = (dt.Verificar_SSNRFC == "yes") ? true : false;
            checkIMAC.Checked = (dt.Es_IMAC == "yes") ? true : false;
            rCheckAssetSi.Checked = (dt.Verificar_Asset == "yes") ? false : false;
            rCheckAssetNo.Checked = (dt.Verificar_Asset == "yes") ? false : false;
            checkTicket.Checked = true;            
        }

        #endregion


        private void checkTicket_CheckedChanged(object sender, EventArgs e)
        {
            if (checkTicket.Checked)
                cajaTicket.Visible = false;
            else
                cajaTicket.Visible = true;
        }

        private void checkAsset_CheckedChanged(object sender, EventArgs e)
        {
            if (checkAsset.Checked)
            {
                rCheckAssetSi.Visible = true;
                rCheckAssetNo.Visible = true;
            }
            else
            {
                rCheckAssetSi.Visible = false;
                rCheckAssetNo.Visible = false;
            }
        }

        private void cajaKeyword_TextChanged(object sender, EventArgs e)
        {
            switch (cajaKeyword.Text)
            {
                case "STEUAW":
                    cajaTitulo.Text += ", STEUAW";
                    break;

                case "XEROX":
                    cajaTitulo.Text += ", XEROX";
                    break;

                default:
                    break;
            }

        }

        private void checkIMAC_CheckedChanged(object sender, EventArgs e)
        {
            cajaSeveridad.Text = (checkIMAC.Checked) ? "5" : ""; 
        }

        private void doMinimizar_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
